function dm_bef_aft_plot_ttest_withmoreinfo(dm_array_m2s, dm_array_s2m, aligned_x_axis, gvkey_vec_m2s, gvkey_vec_s2m)

%%% run [validvec, ismultivec, dm_array_m2s, dm_array_s2m, aligned_x_axis, gvkey_vec_m2s, gvkey_vec_s2m] = dm_before_after_div() to get inputs for this function

medianline_m2s = prctile(log(dm_array_m2s), 50, 2);
upperline_m2s = medianline_m2s + std(log(dm_array_m2s), 0, 2, 'omitmissing');
lowerline_m2s = medianline_m2s - std(log(dm_array_m2s), 0, 2, 'omitmissing');

upperline_s2m = prctile(log(dm_array_s2m), 75, 2);
lowerline_s2m = prctile(log(dm_array_s2m), 25, 2);
medianline_s2m = prctile(log(dm_array_s2m), 50, 2);

close all
figure
set(gcf, 'Position', [50 90 1500 800]*0.6)
set(gcf, 'Color', 'w');

hold on
plot(categorical(aligned_x_axis), medianline_m2s, 'LineWidth', 1.5, 'Color', 'blue', 'LineStyle','-');
plot(categorical(aligned_x_axis), medianline_s2m, 'LineWidth', 1.5, 'Color', 'red', 'LineStyle','-.');
hold off
grid minor
ylabel('log (\textit{dm})','Interpreter','latex')
xlabel('Years to (de)diversification','Interpreter','latex')
% xticks(aligned_x_axis)
box off
legend({'Multi-division $\rightarrow$ Stand-alone', 'Stand-alone $\rightarrow$ Multi-division'}, 'Location', 'northeast', 'FontSize', 10, 'Interpreter','latex')
legend boxoff

jf = java.text.DecimalFormat;

%%%%%%%%%%%%%%

texstr = ['\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{@{}lccccccccc@{}} ' ...
    '\toprule ' ...
    ' & \multicolumn{4}{c}{Multi-division $\rightarrow$ Stand-alone} &  & \multicolumn{4}{c}{Stand-alone $\rightarrow$ Multi-division} \\ ' ...
    ' & Before (B) & After (A) & t-test (A - B) & \# of obs & & Before (B)  & After (A)   & t-test (A - B) & \# of obs \\ \cmidrule(l){2-5} \cmidrule(l){6-10} '];

for yr = 1:4
    if yr ==1 
        yrstr = ' year';
    else
        yrstr = ' years';
    end
    m2s_b_vec = dm_array_m2s(5-yr:4, :);
    m2s_a_vec = dm_array_m2s(5:4+yr, :);
    m2s_b_mean = mean(log(m2s_b_vec(:)), 'omitmissing');
    m2s_a_mean = mean(log(m2s_a_vec(:)), 'omitmissing');
    m2s_diff = m2s_a_mean - m2s_b_mean;
    [~, p_m2s] = ttest(log(m2s_a_vec(:)) - log(m2s_b_vec(:)));
    nobs_m2s = sum(isfinite([m2s_a_vec(:); m2s_b_vec(:)]));
    if p_m2s < 0.1
        m2s_star = ['\sym{' stars_given_pval(p_m2s) '}'];
    else
        m2s_star = [];
    end

    s2m_b_vec = dm_array_s2m(5-yr:4, :);
    s2m_a_vec = dm_array_s2m(5:4+yr, :);
    s2m_b_mean = mean(log(s2m_b_vec(:)), 'omitmissing');
    s2m_a_mean = mean(log(s2m_a_vec(:)), 'omitmissing');
    s2m_diff = s2m_a_mean - s2m_b_mean;
    [~, p_s2m] = ttest(log(s2m_a_vec(:)) - log(s2m_b_vec(:)));
    nobs_s2m = sum(isfinite([s2m_a_vec(:); s2m_b_vec(:)]));
    if p_s2m < 0.1
        s2m_star = ['\sym{' stars_given_pval(p_s2m) '}'];
    else
        s2m_star = [];
    end

    texstr = [texstr num2str(yr) yrstr ' away & ' num2str(m2s_b_mean, '%.2f') ' & ' num2str(m2s_a_mean, '%.2f') ' & ' num2str(m2s_diff, '%.2f') m2s_star ' & ' char(jf.format(nobs_m2s)) ' & & ' num2str(s2m_b_mean, '%.2f') ' & ' num2str(s2m_a_mean, '%.2f') ' & ' num2str(s2m_diff, '%.2f') s2m_star  ' & ' char(jf.format(nobs_s2m)) ' \\'];
end
texstr = [texstr ' \midrule '];
nfirms_m2s = length(unique(gvkey_vec_m2s));
nfirms_s2m = length(unique(gvkey_vec_s2m));
texstr = [texstr ' \# of firms & \multicolumn{4}{c}{' char(jf.format(nfirms_m2s)) '} &  & \multicolumn{4}{c}{' char(jf.format(nfirms_s2m)) '} \\ '];

texstr = [texstr ' \bottomrule ' ...
    '\end{tabular}'];

writelines(texstr, "..\output\table_4_pseudo.tex")